#include <iostream>

using std::cout;
using std::endl;

//i^2 = -1

class Complex
{
public:
    Complex(double dreal = 0.0, double dimag = 0.0)
    : _dreal(dreal)
    , _dimag(dimag)
    {
        cout << "Complex(double = 0.0, double = 0.0)" << endl;
    }

    void print() const
    {
        cout << _dreal << " + " << _dimag << "i" << endl;
    }

    double getReal() const
    {
        return _dreal;
    }

    double getImag() const
    {
        return _dimag;
    }

    ~Complex()
    {
        cout << "~Complex()" << endl;
    }
    friend Complex operator+(const Complex &lhs, const Complex &rhs);
private:
    double _dreal;
    double _dimag;
};

//运算符重载之友元函数
Complex operator+(const Complex &lhs, const Complex &rhs)
{
    cout << "friend Complex operator+(const, const)" << endl;
    return Complex(lhs._dreal + rhs._dreal, 
                   lhs._dimag + rhs._dimag);
}
void test()
{
    int a = 10;
    int b = 20;
    int c = a + b;//30

    Complex com1(1, 2);
    cout << "com1 = ";
    com1.print();

    cout << endl;
    Complex com2(3, 4);
    cout << "com2 = ";
    com2.print();

    cout << endl;
    Complex com3 = com1 + com2;
    cout << "com3 = ";
    com3.print();
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

